package com.t2t.top.action.$21visitor.ext;

import java.util.*;

/**
 * Created by yangpengfei on 2016/9/21.
 */
public class Test {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new HashMap());
        list.add("a");
        list.add(1.0F);
        list.add(2);
        list.add(2.0D);
        list.add(new Object());

        new Test().messyPrintCollection(list);
        System.out.println("====================");
        new Test().messyPrintCollectionExt(list);
    }

    public void messyPrintCollection(Collection collection) {
        Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            if (o instanceof Collection)
                messyPrintCollection((Collection) o);
            else if (o instanceof String)
                System.out.println("'" + o.toString() + "'");
            else if (o instanceof Float)
                System.out.println(o.toString() + "f");
            else {
                System.out.println(o.toString());
            }
        }
    }

    public void messyPrintCollectionExt(Collection collection) {
        PrintVisitor visitor = new PrintVisitor();

        Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            try {
                visitor.visit(o);
            } catch (Exception e) {
                System.out.println("处理不了!" + o);
            }
        }
    }

}
